我對所有這些都陌生,所以我開始編寫這個應用程序來學習。在Android片段中,沒有聲音在onClick事件後播放
該應用程序從6個可點擊的圖像開始,導致由一系列「可滑動」屏幕組成的輔助活動,每個屏幕上都有一個圖像,點擊時會產生聲音。下面是二次和片段代碼需要生成的畫面的代碼:
SecondaryPage.java
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class SecondaryPage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.secondary);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
}
}
SimpleFragmentAdapter.java
AnimalsFragement.java(Croc()
和Camel()
是相同的,除了名稱)
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import static com.example.testfrags.R.layout.animals;
public class AnimalsFragment extends Fragment {
private ImageView imageView;
private MediaPlayer mp;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = null;
//Inflating View to access it's resources
v = inflater.inflate(animals, container, false);
Log.i("AnimalsFragment", "mp before playing: " + mp);
//creating clickable image
imageView = (ImageView) v.findViewById(R.id.jungle_animals);
// Sound played once clicked
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start playback.
mp = MediaPlayer.create(getActivity(), R.raw.lioncub2);
Log.i("AnimalsFragment", "mp: " + mp);
mp.start();
mp.stop();//Stops playback - added precaution
// release media resources
releaseMediaPlayer();
Log.i("AnimalsFragment", "mp after release: " + mp);
// Little msg confirm end of sound
Toast.makeText(v.getContext(), "Questo era il Leoncino", Toast.LENGTH_SHORT).show();
}
});
return v;
} // end of onCreatView
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mp != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mp.reset();
mp.release();
// Set the media player back to null.
} // end of if
} // end of releaseMediaPlayer
@Override
public void onStop() {
super.onStop(); // Always call the superclass method first
releaseMediaPlayer();
}
}
這些是xml文件: 秒ondary.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
animals.xml(croc.xml和camel.xml相同)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/jungle_animals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/lion"/>
</LinearLayout>
的應用程序編譯和運行,但是當我在任何片段點擊所有圖象沒有聲音播放。我已經在我的設備上嘗試過,但是沒有任何效果。
我做錯了什麼?
我試圖在符合v.getContext()
改變getActivity()
:
`mp = MediaPlayer.create(getActivity(), R.raw.lioncub2);`
,但沒有任何變化。 所有聲音文件都是mp3。我還寫了一個secondaryPage.java的版本,但沒有包含三個圖像的片段,只是爲了查看mp3文件是否可以正常工作並且它們都正常播放。 我還插入了Toast
消息,以檢查onClick()
方法是否正常工作。 當我調試並進入mp = MediaPlayer.create(getActivity(), R.raw.lioncub2);
行並繼續時,播放聲音。
這裏是Log.i語句的輸出:
10-15 10:50:36.962 2459-2459/com.example.testfrags I/Croc: mp before playing: null
10-15 10:50:37.682 2459-2459/com.example.testfrags I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
10-15 10:50:39.472 2459-2459/com.example.testfrags I/AnimalsFragment: mp: [email protected]
10-15 10:50:39.482 2459-2459/com.example.testfrags I/AnimalsFragment: mp after release: null
在此先感謝您的幫助!
非常感謝。我刪除了停止方法,但沒有任何改變。以前的MediaPlayer(沒有片段編碼)在重現文件時沒有問題,所以我不明白爲什麼它現在不能工作。 – BletchleyP
我會先給你回答,告訴我你想玩的音頻類型是短還是長? –
你真好。音頻文件是mp3文件中的動物聲音,所以不會太長。 – BletchleyP