2016-07-25 31 views
0

嗨,所以即時開始與基本的音樂應用程序,不要恨我不使用recyclerview。但是,該應用程序運行良好,但沒有力量關閉,但是當我點擊歌曲不會播放的歌曲。 Android的監聽器說「 音樂應用測試1:setDataSource失敗」 林不知道我的問題是否與我的目錄有關,所以讓我知道。 如果您需要查看xml文件,還請告知我。Android媒體播放器(設置dataSource失敗)

package com.example.abhishek.musicapptest1; 

import android.annotation.SuppressLint; 
import android.app.ListActivity; 
import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.app.Activity; 
import android.view.View.OnClickListener; 
import android.view.View.OnHoverListener; 

import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 

import java.io.File; 
import java.io.FilenameFilter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

class Mp3filter implements FilenameFilter {// filters all files that are mp3 

    @Override 
    public boolean accept(File directory, String song_name) { // will return mp3 
     return (song_name.endsWith(".mp3")); 
    } 
} 

public class MainActivity extends ListActivity { 

    private static final String SD_PATH = new String("/sdcard/Music/Phone Music"); 

    private List<String> songs = new ArrayList<String>(); 
    private MediaPlayer mp = new MediaPlayer(); 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     //setSupportActionBar(toolbar); 

     updatePlaylist(); 

     Button stopPlay = (Button) findViewById(R.id.stopBtn); 
     stopPlay.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mp.stop(); 
      } 

     }); 


     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    protected void onListItemClick(ListView list, View view, int position, long id) { 
     try { 
      mp.reset(); 
      mp.setDataSource(SD_PATH + songs.get(position)); 
      mp.prepare(); 
      mp.start(); 
     } catch (IOException e) { 
      Log.v(getString(R.string.app_name),e.getMessage()); 
     } 
    } 

    private void updatePlaylist() {// will update the play list, will find the sd card 
     // we need to filter out the files which we dont want 

     File home = new File(SD_PATH); 
     if (home.listFiles(new Mp3filter()).length > 0) { 
      for (File file : home.listFiles(new Mp3filter())) { 
       songs.add(file.getName()); 
      } 

      ArrayAdapter<String> songList = new ArrayAdapter<String>(this, R.layout.song_view, songs); 
      setListAdapter(songList); 

     } 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.abhishek.musicapptest1/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://com.example.abhishek.musicapptest1/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 
} 

回答

1

從您的代碼,檢查這些線:

private static final String SD_PATH = new String("/sdcard/Music/Phone Music"); 
songs.add(file.getName()); 
mp.setDataSource(SD_PATH + songs.get(position)); 

因此您的數據源是不是一個有效的文件名,反斜槓丟失。

+0

謝謝了。 – user314676