質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

916閲覧

Androidアプリ web上の音声ファイルの再生時間を取得したい

takepon0209

総合スコア34

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2019/02/19 09:22

編集2019/02/19 13:29

前提・実現したいこと

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になってしまっていると思います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2019/02/19 13:03 編集

android のバージョンは幾つでしょうか. また, 再生された「外web上の音声ファイル」は この URL のファイルでしょうか. その時のコードを実行できる形でご提示願えますか?
takepon0209

2019/02/19 13:31

バージョン(APIのことでしょうか?)は28です。 コードを修正しました。
guest

回答1

0

ベストアンサー

コード等ありがとうございました.
実行してみましたが, 確かに mp.getDuration() では値が取れていませんでした. が, 0 では無く -1 でした.

prepare や OnPreparedListener 等試してみましたが, 全て -1 が返されています.
ドキュメント上では getDuration は prepare や start 後であれば(そしてストリーミングで無ければ?)値を返すことになっているようですので, 申し訳ありません, 私が調べた限りではダウンロードするしか getDuration で値を得る方法は無いようです.

投稿2019/02/20 07:30

jimbe

総合スコア12545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takepon0209

2019/02/20 12:21

承知しました。 調べてくださりありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問