安卓工作室中的媒体播放器出现问题



我似乎无法让媒体播放器在我的应用程序中播放音乐。我设法让它在另一个项目中工作,但似乎无法在当前项目中正确实现它。每次我点击音乐标签,都会导致应用程序崩溃,并打印出下面的错误消息。如有任何帮助,我们将不胜感激。非常感谢。

package com.example.android.japaneselanguageapp;
public class Word {
/*Default translation for the word*/
private String jDefaultTranslation;
/*Japanese translation for the word*/
private String jJapaneseTranslation;
/*Image resource ID for the word*/
private int jimageResourceId = NO_IMAGE;
/*Constant to set no image state */
private static final int NO_IMAGE = -1;
/*resource id for word*/
private int mAudioId;


public Word(String defaultTranslation, String japaneseTranslation, int audioResourceId){
jDefaultTranslation = defaultTranslation;
jJapaneseTranslation = japaneseTranslation;
mAudioId = audioResourceId;
}
/*Overloaded Constructor for everything needing images*/
public Word(String defaultTranslation, String japaneseTranslation, int imageResourceId, int audioResourceId){
jDefaultTranslation = defaultTranslation;
jJapaneseTranslation = japaneseTranslation;
jimageResourceId = imageResourceId;
mAudioId = audioResourceId;
}
public int getaudioResourceId(){
return mAudioId;
}

package com.example.android.japaneselanguageapp;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
public class NumbersActivity extends AppCompatActivity {
private MediaPlayer mMediaPlayer;
/*listener will be triggered when mediaplayer finishes*/
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Now that the sound file has finished playing, release the media player resources.
releaseMediaPlayer();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.words_list);
/*Create an array list of words*/
final ArrayList<Word> words = new ArrayList<>();
/* the last parameter is switched by added audio files to raw folder and declaring*/
words.add(new Word("One", "Ichi", R.drawable.japanese_flag, R.raw.outkast));
WordAdapter adapter = new WordAdapter(this, words);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);

// Register the onClick listener with the implementation above
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Word word = words.get(position);
/*releases if currently existing, to play new media*/
releaseMediaPlayer();
mMediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getaudioResourceId());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}});
}
@Override
protected void onStop() {
super.onStop();
/*when activity is stopped, releases media player so there isn't any sound being played*/
releaseMediaPlayer();
}

/* releases mediaplayer*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}


The log cat error below:

E/MediaPlayerNative: error (1, -2147483648)
D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1282)
at android.media.MediaPlayer.create(MediaPlayer.java:983)
at android.media.MediaPlayer.create(MediaPlayer.java:954)
at com.example.android.japaneselanguageapp.NumbersActivity$2.onItemClick(NumbersActivity.java:83)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
at android.widget.AbsListView$3.run(AbsListView.java:4052)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.japaneselanguageapp, PID: 17776
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.example.android.japaneselanguageapp.NumbersActivity$2.onItemClick(NumbersActivity.java:84)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
at android.widget.AbsListView$3.run(AbsListView.java:4052)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Application terminated.

我知道你解决了你的问题,但以防万一我想和你分享这个类。它是一个助手类,允许您在应用程序中的任何位置播放声音。

这就是类:

package com.oyduts.blindtest.Helpers;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.widget.Toast;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
public class MediaManager
{
private static MediaPlayer _mediaPlayer;
private static Context _context;
private static int _pausedAt;
private static CountDownTimer _countDownTimer;
public static Bitmap GetBitmapFromAssets(Context c, String filename)
{
AssetManager am = c.getAssets();
InputStream is = null;
try
{
is = am.open(filename);
}
catch (IOException e)
{
e.printStackTrace();
}
return BitmapFactory.decodeStream(is);
}
public static void InitMediaPlayer(Context c)
{
_context = c;
_mediaPlayer = new MediaPlayer();
}
public static void LoadMusic(String filePath)
{
try
{
_mediaPlayer.reset();
AssetFileDescriptor afd = _context.getAssets().openFd(filePath);
_mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
_mediaPlayer.prepare();
afd.close();
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(_context, "Couldn't load the music, please check your data folder.", Toast.LENGTH_SHORT).show();
}
}
public static void PauseMusic()
{
try
{
if (_mediaPlayer != null && _mediaPlayer.isPlaying())
{
_pausedAt = _mediaPlayer.getCurrentPosition();
_mediaPlayer.pause();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void PlayMusic()
{
try
{
if (_mediaPlayer != null && !_mediaPlayer.isPlaying()) _mediaPlayer.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void ResumeMusic()
{
try
{
if (_mediaPlayer != null && !_mediaPlayer.isPlaying())
{
_mediaPlayer.seekTo(_pausedAt);
_mediaPlayer.start();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void StopMusic()
{
try
{
if (_mediaPlayer != null && _mediaPlayer.isPlaying()) _mediaPlayer.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void ReleaseMusic()
{
try
{
if (_mediaPlayer != null) _mediaPlayer.release();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

还有一个关于如何使用它的例子:

首先,在onCreate方法中初始化播放器:

// Init mediaplayer
MediaManager.InitMediaPlayer(this);

然后,如果你想在实际播放之前加载声音:

MediaManager.LoadMusic("your_file_path");

然后您可以使用播放音乐

MediaManager.PlayMusic();

停止音乐:

MediaManaer.StopMusic();

您也可以恢复音乐(例如,在onResume方法中(:

MediaManager.ResumeMusic();

最后,不要忘记在最后释放玩家:

MediaManager.ReleaseMusic();

您也可以根据需要修改此类。

编辑顺便说一下,您的音频文件应该在应用程序的资产文件夹中

致以最诚挚的问候

相关内容

  • 没有找到相关文章

最新更新