前提・実現したいこと
androidアプリで、web上の音声ファイルの再生時間を取得したいです。
発生している問題・エラーメッセージ
getDurationメソッドで再生時間を取得しても、値が0となってしまいます。
該当のソースコード
AndroidManifest.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.android_test.babesnaky"> 4 5 <uses-permission android:name="android.permission.INTERNET" /> 6 <application 7 android:allowBackup="true" 8 android:icon="@mipmap/ic_launcher" 9 android:label="@string/app_name" 10 android:roundIcon="@mipmap/ic_launcher_round" 11 android:supportsRtl="true" 12 android:theme="@style/AppTheme"> 13 14 <!-- <activity android:name=".FutatsumeActivity"></activity> --> 15 <!-- <activity android:name=".MittsumeActivity"></activity> --> 16 <activity android:name=".MusicActivity"> 17 <intent-filter> 18 <action android:name="android.intent.action.MAIN" /> 19 20 <category android:name="android.intent.category.LAUNCHER" /> 21 </intent-filter> 22 </activity> 23 <!--<activity android:name=".YottsumeActivity" />--> 24 <!--<activity android:name=".MusicActivity"></activity>--> 25 </application> 26</manifest>
activity_music.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MusicActivity"> 8 9 <LinearLayout 10 android:id="@+id/linearLayout" 11 android:layout_width="361dp" 12 android:layout_height="59dp" 13 android:layout_alignParentTop="true" 14 android:layout_marginStart="8dp" 15 android:layout_marginLeft="8dp" 16 android:layout_marginTop="12dp" 17 android:layout_marginEnd="8dp" 18 android:layout_marginRight="8dp" 19 android:layout_marginBottom="8dp" 20 android:gravity="start" 21 android:orientation="horizontal" 22 app:layout_constraintBottom_toTopOf="@+id/linearLayout3" 23 app:layout_constraintEnd_toEndOf="parent" 24 app:layout_constraintStart_toStartOf="parent" 25 app:layout_constraintTop_toTopOf="parent"> 26 27 <TextView 28 android:id="@+id/title" 29 android:layout_width="340dp" 30 android:layout_height="wrap_content" 31 android:layout_marginStart="8dp" 32 android:layout_marginLeft="8dp" 33 android:layout_marginEnd="8dp" 34 android:layout_marginRight="8dp" 35 android:layout_marginBottom="8dp" 36 android:layout_weight="1" 37 android:text="タイトル" 38 app:layout_constraintBottom_toBottomOf="parent" 39 app:layout_constraintEnd_toEndOf="parent" 40 app:layout_constraintStart_toStartOf="parent" 41 app:layout_constraintTop_toTopOf="parent" 42 app:layout_constraintVertical_bias="0.0" /> 43 </LinearLayout> 44 45 <LinearLayout 46 android:id="@+id/linearLayout3" 47 android:layout_width="368dp" 48 android:layout_height="53dp" 49 android:layout_below="@+id/linearLayout" 50 android:layout_marginStart="8dp" 51 android:layout_marginLeft="8dp" 52 android:layout_marginTop="24dp" 53 android:layout_marginEnd="8dp" 54 android:layout_marginRight="8dp" 55 android:orientation="horizontal" 56 app:layout_constraintBottom_toTopOf="@+id/linearLayout4" 57 app:layout_constraintEnd_toEndOf="parent" 58 app:layout_constraintStart_toStartOf="parent"> 59 60 <SeekBar 61 android:id="@+id/positionBar" 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" 64 android:layout_weight="1" /> 65 </LinearLayout> 66 67 <LinearLayout 68 android:id="@+id/linearLayout4" 69 android:layout_width="wrap_content" 70 android:layout_height="95dp" 71 android:layout_below="@+id/linearLayout3" 72 android:layout_alignParentBottom="true" 73 android:layout_marginStart="8dp" 74 android:layout_marginLeft="8dp" 75 android:layout_marginTop="33dp" 76 android:layout_marginEnd="8dp" 77 android:layout_marginRight="8dp" 78 android:layout_marginBottom="227dp" 79 android:orientation="horizontal" 80 app:layout_constraintBottom_toBottomOf="parent" 81 app:layout_constraintEnd_toEndOf="parent" 82 app:layout_constraintStart_toStartOf="parent"> 83 84 <Button 85 android:id="@+id/playButton" 86 android:layout_width="wrap_content" 87 android:layout_height="wrap_content" 88 android:layout_marginStart="104dp" 89 android:layout_marginLeft="104dp" 90 android:layout_marginTop="8dp" 91 android:layout_marginBottom="8dp" 92 android:text="再生" 93 app:layout_constraintBottom_toBottomOf="parent" 94 app:layout_constraintStart_toStartOf="parent" 95 app:layout_constraintTop_toTopOf="parent" 96 app:layout_constraintVertical_bias="0.501" /> 97 98 <Button 99 android:id="@+id/stopButton" 100 android:layout_width="87dp" 101 android:layout_height="wrap_content" 102 android:layout_marginTop="8dp" 103 android:layout_marginEnd="104dp" 104 android:layout_marginRight="104dp" 105 android:layout_marginBottom="8dp" 106 android:text="停止" 107 app:layout_constraintBottom_toBottomOf="parent" 108 app:layout_constraintEnd_toEndOf="parent" 109 app:layout_constraintTop_toTopOf="parent" 110 app:layout_constraintVertical_bias="0.501" /> 111 </LinearLayout> 112 113</RelativeLayout>
MusicActivity.java
java
1package com.android_test.babesnaky; 2 3import android.media.MediaPlayer; 4import android.net.Uri; 5import android.support.v7.app.AppCompatActivity; 6import android.os.Bundle; 7import android.view.View; 8import android.widget.Button; 9import android.widget.SeekBar; 10 11public class MusicActivity extends AppCompatActivity implements View.OnClickListener { 12 13 private MediaPlayer mp; 14 private String path; 15 private SeekBar positionBar; 16 int totalTime; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_music); 22 Button playButton = (Button)findViewById(R.id.playButton); 23 Button stopButton = (Button)findViewById(R.id.stopButton); 24 playButton.setOnClickListener(this); 25 stopButton.setOnClickListener(this); 26 27 path = "https://maoudamashii.jokersounds.com/music/song/preview/song_kei_vegalost.mp3"; 28 29 mp = MediaPlayer.create(this, Uri.parse(path)); 30 totalTime = mp.getDuration(); 31 mp.start(); 32 33 34 positionBar = findViewById(R.id.positionBar); 35 positionBar.setMax(totalTime); 36 positionBar.setOnSeekBarChangeListener( 37 new SeekBar.OnSeekBarChangeListener() { 38 @Override 39 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 40 if (fromUser) { 41 mp.seekTo(progress); 42 positionBar.setProgress(progress); 43 } 44 } 45 46 @Override 47 public void onStartTrackingTouch(SeekBar seekBar) { 48 49 } 50 51 @Override 52 public void onStopTrackingTouch(SeekBar seekBar) { 53 54 } 55 } 56 ); 57 } 58 59 public void onClick(View v){ 60 if(v.getId() == R.id.playButton){ 61 mp.start(); 62 }else if (v.getId() == R.id.stopButton){ 63 mp.pause(); 64 } 65 } 66}
試したこと
・rawフォルダの音声ファイルの再生時間は取得できます。
・<uses-permission android:name="android.permission.INTERNET" />は設定しており、
再生であればweb上の音声ファイルでも問題なくできます。
###予測
恐らくですが、web上の音声ファイルのストリーミング中に再生時間を取ってきてしまい、
値が0になってしまっていると思います。
回答1件
あなたの回答
tips
プレビュー