【问题标题】:Playing a downloaded file with a MediaPlayer service, Android使用 MediaPlayer 服务播放下载的文件,Android
【发布时间】:2012-06-12 00:30:00
【问题描述】:

所以我正在为我的移动编程课程创建一个应用程序,但我在其中的“游戏”部分遇到了困难。给你一些背景信息:基本上我们需要创建一个基本的音频播放器,它从预定义的源下载 mp3,然后播放该 mp3,等等。我已经正确设置了下载服务,但我很困惑为什么我播放文件的代码不起作用。在我的 main.java 中,我有很多匿名用户。 UI 按钮的 onclick 侦听器,然后在我的媒体播放器服务中使用 switch 语句来确定单击了哪个按钮。

这是我的主要活动中播放选项的 onclick:

play.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), PlayService.class);
            File path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
            File file = new File(path, "Bob_Marley-Jammin.mp3");
            intent.putExtra("path", file);
            intent.putExtra("key", 0);
            startService(intent);               
        }
    });

这里是点击下载:

 download.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), DownloadService.class);
            intent.putExtra("url", "https://dl.dropbox.com/s/6e6pn43916nl10i/Bob_Marley-Jammin.mp3?dl=1");
            startService(intent);               
        }
    });

这是我的 PlayService:

package com.connor.black;

import java.io.IOException;

import android.app.IntentService;
import android.content.Intent;
import android.media.MediaPlayer;

public class PlayService extends IntentService{

public PlayService() {
    super("PlayService");
}

public MediaPlayer mMediaPlayer;
public String path;

@Override
protected void onHandleIntent(Intent intent) {
    path = intent.getStringExtra(path);
    int key = intent.getIntExtra("key", 0);
    mMediaPlayer = new MediaPlayer();

    switch (key) {
    case 0: //Play
        if (mMediaPlayer.isPlaying())
            break;
        else{
            try {
                mMediaPlayer.setDataSource(path);
            } catch (IllegalArgumentException e1) {
                e1.printStackTrace();
            } catch (SecurityException e1) {
                e1.printStackTrace();
            } catch (IllegalStateException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                mMediaPlayer.prepare();
            } catch (IllegalStateException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
                mMediaPlayer.start();
        }
        break;
    case 1: //Pause
        if(mMediaPlayer.isPlaying())
            mMediaPlayer.pause();
        break;
    case 2: //Stop
        if(mMediaPlayer.isPlaying()){
            mMediaPlayer.pause();
            mMediaPlayer.seekTo(0);
        }
        break;
    default:
        break;
    }

}

}

基本上文件下载正确,但是当我按下“播放”按钮时没有任何反应。

【问题讨论】:

    标签: android service media-player


    【解决方案1】:

    你的函数被调用了吗?使用 Log.d 进行检查。您的 mp3 路径是否正确?

    下面的小sn-p播放MP3(改变你的文件路径)

    package com.aplayer;
    
    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    
    public class APlayerActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
            MediaPlayer mMediaPlayer = new MediaPlayer();
    
            Uri data;
            data = Uri.parse("/sdcard/jazz.mp3");
    
            mMediaPlayer = MediaPlayer.create(getBaseContext(), data);
            mMediaPlayer.setLooping(false); // Set looping
            mMediaPlayer.start();
    }
    

    }

    【讨论】:

    • 我正在使用此代码,但来自服务内部。我应该将 getBaseContext() 更改为其他内容吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多