首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MediaPlayer服务播放下载的文件

使用MediaPlayer服务播放下载的文件
EN

Stack Overflow用户
提问于 2012-06-12 08:30:00
回答 1查看 1.5K关注 0票数 1

因此,我正在为我的移动编程类创建一个应用程序,并且遇到了它的“播放”部分的困难。给你一些背景:基本上,我们需要创建一个基本的音频播放器,它从预定义的源下载一个mp3,然后播放该mp3,等等。我已经正确地设置了下载服务,但是我不明白为什么我播放文件的代码不能工作。在我的main.java中,我有一大堆anon。在我的mediaplayer服务中,单击UI按钮的onclick侦听器,然后执行switch语句,以确定单击了哪个按钮。

以下是我的主要活动中的play选项的onclick:

代码语言:javascript
复制
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);               
        }
    });

下面是onclick的下载:

代码语言:javascript
复制
 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:

代码语言:javascript
复制
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;
    }

}

}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-12 09:26:32

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

下面的小片段播放一个MP3 (更改文件的路径)

代码语言:javascript
复制
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();
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10989168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档